//
//  Sprite.m
//  phantom2d
//
//  Created by Jorge Lorenzon on 4/11/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "F2DSprite.h"


@implementation F2DSprite

@synthesize atlas = _atlas;
@synthesize spriteName = _spriteName;

//- (Sprite*) init
//{
//    self = [super init];
//    if (self) {
//        const int X = 0;
//        const int Y = 1;
//        
//        _vertices[0].Position[X] = -0.5f;
//        _vertices[0].Position[Y] = -0.5f;
//        
//        _vertices[1].Position[X] = 0.5f;
//        _vertices[1].Position[Y] = -0.5f;
//        
//        _vertices[2].Position[X] = -0.5f;
//        _vertices[2].Position[Y] = 0.5f;        
//        
//        _vertices[3].Position[X] = 0.5f;
//        _vertices[3].Position[Y] = 0.5f;        
//    }
//    return self;
//}

- (void) draw 
{
    glDisable(GL_DEPTH_TEST);
    glDepthMask(FALSE);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glEnable(GL_TEXTURE_2D);
    glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    
    //habilitar vertex array (posiciones de vertices)
    glEnableClientState(GL_VERTEX_ARRAY);    
    glEnableClientState(GL_COLOR_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);    
    
    //decirle a la GPU donde se encuentran las posiciones de los vertices
    glVertexPointer(2, GL_FLOAT, sizeof(Vertex), &_vertices[0].Position);
    
    glColorPointer(4, GL_FLOAT, sizeof(Vertex), &_vertices[0].Color);

    glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &_vertices[0].UV);
    
    glBindTexture(GL_TEXTURE_2D, _atlas.texture.name);

    
    //aplicar la transformacion del objeto
    glPushMatrix();
    
   // F2DTransform* t = self.owner.transform;
    
    ///glTranslatef(t.position.x, t.position.y, 0);
    glRotatef(0, 0, 0, 1);
    //glScalef(t.scale.width, t.scale.height, 1);
    
    //dibujar
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    
    glPopMatrix();
}

- (void) setSpriteName:(NSString *)spriteName
{
    _spriteName = spriteName;
    
    if (_atlas != nil)
    {
        SpriteData* spriteData = [_atlas spriteDataFrom:spriteName];
        
        if (spriteData != nil)
        {
            //set vertex position
            _vertices[0].Position[0] = -spriteData->rect.size.width * 0.5f ;
            _vertices[0].Position[1] = -spriteData->rect.size.height * 0.5f;
            
            _vertices[1].Position[0] = spriteData->rect.size.width * 0.5f;
            _vertices[1].Position[1] = -spriteData->rect.size.height * 0.5f;
            
            _vertices[2].Position[0] = -spriteData->rect.size.width * 0.5f;
            _vertices[2].Position[1] = spriteData->rect.size.height * 0.5f;        
            
            _vertices[3].Position[0] = spriteData->rect.size.width * 0.5f;
            _vertices[3].Position[1] = spriteData->rect.size.height * 0.5f;        
            
            //set vertex uvs
            CGSize atlasSize = [_atlas size];
            
            _vertices[0].UV[0] = spriteData->rect.origin.x / atlasSize.width;
            _vertices[0].UV[1] = spriteData->rect.origin.y / atlasSize.height;

            _vertices[1].UV[0] = (spriteData->rect.origin.x + spriteData->rect.size.width) / atlasSize.width;
            _vertices[1].UV[1] = _vertices[0].UV[1];
            
            _vertices[2].UV[0] = _vertices[0].UV[0];
            _vertices[2].UV[1] = (spriteData->rect.origin.y + spriteData->rect.size.height) / atlasSize.height;            

            _vertices[3].UV[0] = _vertices[1].UV[0];
            _vertices[3].UV[1] = _vertices[2].UV[1];
//            _vertices[0].UV[0] = 0;
//            _vertices[0].UV[1] = 0;
//
//            _vertices[1].UV[0] = 1;
//            _vertices[1].UV[1] = 0;
//            
//            _vertices[2].UV[0] = 0;
//            _vertices[2].UV[1] = 1;
//
//            _vertices[3].UV[0] = 1;
//            _vertices[3].UV[1] = 1;
            
            
            _vertices[0].Color[0] = 1;
            _vertices[0].Color[1] = 1;
            _vertices[0].Color[2] = 1;            
            _vertices[0].Color[3] = 1;                        
            
            _vertices[1].Color[0] = 1;
            _vertices[1].Color[1] = 1;
            _vertices[1].Color[2] = 1;            
            _vertices[1].Color[3] = 1;                        

            _vertices[2].Color[0] = 1;
            _vertices[2].Color[1] = 1;
            _vertices[2].Color[2] = 1;            
            _vertices[2].Color[3] = 1;                        

            _vertices[3].Color[0] = 1;
            _vertices[3].Color[1] = 1;
            _vertices[3].Color[2] = 1;            
            _vertices[3].Color[3] = 1;                        
        }
    }
}
- (void) update
{
    [self update];
}
@end
